package GUI;
import MonteCarloSequential.*;
import MonteCarloConcurrency.*;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

import javax.swing.*; 

public class GUI {
	public static void createAndShowGUI() throws NumberFormatException, IOException {
		//Create and set up the window.
		bar=new JProgressBar();
		final JFrame frame = new JFrame("Monte Carlo");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		final JPanel panel=new JPanel();
		final JPanel panel1=new JPanel();

		//Panel : Form		
		Box panneauDropDown = Box.createHorizontalBox();	

		//Box of label+ checkboxes
		Box startPage = Box.createVerticalBox();
		final JLabel label = new JLabel("Welcome to the Monte Carlo Algorithm interface");
		
		//label
		startPage.add(label);		
		startPage.add(Box.createVerticalStrut(20));
		//Multiple or Single thread checkboxes
		final JCheckBox SingleT=new JCheckBox("Single thread algorithm",true);
		final JCheckBox MultipleT=new JCheckBox("Multiple thread algorithm",false);
		ButtonGroup buttonGroupT = new ButtonGroup();
		buttonGroupT.add(SingleT);
		buttonGroupT.add(MultipleT);
		Box CheckBoxThread=Box.createHorizontalBox();
		CheckBoxThread.add(SingleT);
		CheckBoxThread.add(MultipleT);
		startPage.add(CheckBoxThread);	
		startPage.add(Box.createVerticalStrut(20));
		//Call or Put checkboxes
		final JCheckBox Call=new JCheckBox("Call",true);
		final JCheckBox Put=new JCheckBox("Put",false);
		ButtonGroup buttonGroup = new ButtonGroup();
		buttonGroup.add(Call);
		buttonGroup.add(Put);
		Box CheckBoxPanneau=Box.createHorizontalBox();
		CheckBoxPanneau.add(Call);
		CheckBoxPanneau.add(Put);
		startPage.add(CheckBoxPanneau);
		startPage.add(Box.createVerticalStrut(10));



		//add panel to the frame
		panel.add(startPage);
		panel.add(panneauDropDown);
		frame.getContentPane().add(panel);

		//Panel1 : Final page
		final JLabel label1 = new JLabel("Calculating");
		final JButton restart = new JButton("Start a new calculation");
		final Box FinalBox=Box.createVerticalBox();
		FinalBox.add(Box.createVerticalStrut(50));
		FinalBox.add(label1);
		FinalBox.add(Box.createVerticalStrut(10));
		FinalBox.add(bar);
		FinalBox.add(Box.createVerticalStrut(50));
		FinalBox.add(restart);
		panel1.add(FinalBox);


		Object[][] tab=FromFile(); //on recup les donn�es du fichier
		Object[]tab1;
		int i=0;
		//S
		while(tab[0][i]!=null && i<(tab[0].length-1)){
			i++;
		}
		//si on est sur la ligne la + longue
		if(tab[0].length==i+1 && tab[0][i]!=null)
			i++;

		tab1=new Object[i];
		i=0;
		while(i<tab1.length){
			tab1[i]=tab[0][i];
			i++;
		}
		final JComboBox SList = new JComboBox(tab1);
		Box SVert = Box.createVerticalBox();
		JLabel Slabel=new JLabel("The current price of the option's underlying asset");
		SVert.add(Slabel);
		SVert.add(SList);
		panneauDropDown.add(SVert);
		panneauDropDown.add(Box.createHorizontalStrut(20));
		//X
		i=0;
		while(tab[1][i]!=null && i<(tab[1].length-1)){
			i++;
		}
		if(tab[1].length==i+1 && tab[1][i]!=null)
			i++;
		tab1=new Object[i];
		i=0;
		while(i<tab1.length){
			tab1[i]=tab[1][i];
			i++;
		}
		final JComboBox XList = new JComboBox(tab1);
		Box XVert = Box.createVerticalBox();
		JLabel Xlabel=new JLabel("The strike price of the option");
		XVert.add(Xlabel);
		XVert.add(XList);
		panneauDropDown.add(XVert);
		panneauDropDown.add(Box.createHorizontalStrut(20));
		//T ->ligne 4 - tab[3]
		i=0;
		while(tab[3][i]!=null && i<(tab[3].length-1)){
			i++;
		}
		if(tab[3].length==i+1 && tab[3][i]!=null)
			i++;
		tab1=new Object[i];
		i=0;
		while(i<tab1.length){
			tab1[i]=tab[3][i];
			i++;
		}
		final JComboBox TList = new JComboBox(tab1);
		Box TVert = Box.createVerticalBox();
		JLabel Tlabel=new JLabel("Time to maturity of the option (in years)");
		TVert.add(Tlabel);
		TVert.add(TList);
		panneauDropDown.add(TVert);
		panneauDropDown.add(Box.createHorizontalStrut(20));

		//r ->ligne3 tab[2]
		i=0;
		while(tab[2][i]!=null && i<(tab[2].length-1)){
			i++;
		}
		if(tab[2].length==i+1 && tab[2][i]!=null)
			i++;
		tab1=new Object[i];
		i=0;
		while(i<tab1.length){
			tab1[i]=tab[2][i];
			i++;
		}
		final JComboBox rList = new JComboBox(tab1);
		JLabel rlabel=new JLabel("The discount rate (risk-free interest rate)");
		SVert.add(Box.createVerticalStrut(20));
		SVert.add(rlabel);
		SVert.add(rList);
		//b
		i=0;
		while(tab[4][i]!=null && i<(tab[4].length-1)){
			i++;
		}
		if(tab[4].length==i+1 && tab[4][i]!=null)
			i++;
		tab1=new Object[i];
		i=0;
		while(i<tab1.length){
			tab1[i]=tab[4][i];
			i++;
		}
		final JComboBox bList = new JComboBox(tab1);
		JLabel blabel=new JLabel("The cost of carry rate");
		XVert.add(Box.createVerticalStrut(20));
		XVert.add(blabel);
		XVert.add(bList);
		//v
		i=0;
		while(tab[5][i]!=null && i<(tab[5].length-1)){
			i++;
		}
		if(tab[5].length==i && tab[5][i]!=null)
			i++;
		tab1=new Object[i];
		i=0;
		while(i<tab1.length){
			tab1[i]=tab[5][i];
			i++;
		}
		final JComboBox vList = new JComboBox(tab1);
		JLabel vlabel=new JLabel("The volatility of the underlying asset");
		TVert.add(Box.createVerticalStrut(20));
		TVert.add(vlabel);
		TVert.add(vList);

		//nSteps
		i=0;
		while(tab[6][i]!=null && i<(tab[6].length-1)){
			i++;
		}
		if(tab[6].length==i+1 && tab[6][i]!=null)
			i++;
		tab1=new Object[i];
		i=0;
		while(i<tab1.length){
			tab1[i]=tab[6][i];
			i++;
		}
		final JComboBox nStList = new JComboBox(tab1);
		JLabel nStlabel=new JLabel("Number of steps");
		SVert.add(Box.createVerticalStrut(20));
		SVert.add(nStlabel);
		SVert.add(nStList);
		SVert.add(Box.createVerticalStrut(20));
		//nSimulation
		i=0;
		while(tab[7][i]!=null && i<(tab[7].length-1)){
			i++;
		}
		if(tab[7].length==i+1 && tab[7][i]!=null)
			i++;

		tab1=new Object[i];
		i=0;
		while(i<tab1.length){
			tab1[i]=tab[7][i];
			i++;
		}
		final JComboBox nSiList = new JComboBox(tab1);
		JLabel nSilabel=new JLabel("Number of simulation");
		XVert.add(Box.createVerticalStrut(20));
		XVert.add(nSilabel);
		XVert.add(nSiList);
		XVert.add(Box.createVerticalStrut(20)); 
		//Button               
		JButton submit=new JButton("Calculate");
		TVert.add(Box.createVerticalStrut(20));
		TVert.add(submit);
		TVert.add(Box.createVerticalStrut(20));

		//Action sur button "calculate";
		submit.setMnemonic(KeyEvent.VK_I);//Listener of the button
		submit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.getContentPane().remove(panel);
				frame.getContentPane().add(panel1); //on met le label "calculating" + progress bar
				frame.validate();
				frame.repaint();


				double result[]=new double[2]; //en 0 le resultat, en 1 le temps


				//label.setText("Button 1!");

				if(Call.isSelected()){  
					//Si on est en Single Thread
					if(SingleT.isSelected()){
						result = MonteCarloSimulation.MonteCarloStandardOptionAVG("c",new Double(SList.getSelectedItem().toString()),new Double(XList.getSelectedItem().toString()),new Double(TList.getSelectedItem().toString()),
								new Double(rList.getSelectedItem().toString()),new Double(bList.getSelectedItem().toString()),new Double(vList.getSelectedItem().toString()),
								new Integer(nStList.getSelectedItem().toString()),new Integer(nSiList.getSelectedItem().toString()));
						label1.setText("The result of the options is "+result[0]+" which was calculating in "+result[1]+" by using the single thread algorithm");	
					}
					//Si on est en multi thread
					else{
						try {
							result=MonteCarloConcurrency.MonteCarloStandardOptionAVG("c",new Double(SList.getSelectedItem().toString()),new Double(XList.getSelectedItem().toString()),new Double(TList.getSelectedItem().toString()),
									new Double(rList.getSelectedItem().toString()),new Double(bList.getSelectedItem().toString()),new Double(vList.getSelectedItem().toString()),
									new Integer(nStList.getSelectedItem().toString()),new Integer(nSiList.getSelectedItem().toString()));
							label1.setText("The result of the options is "+result[0]+" which was calculating in "+result[1]+" by using the multiple thread algorithm");	

						} catch (NumberFormatException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} catch (InterruptedException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} catch (ExecutionException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
					//on affiche le resultats 

					FinalBox.remove(bar);            	
				}
				else{
					if(SingleT.isSelected()){
						result = MonteCarloSimulation.MonteCarloStandardOptionAVG("p",new Double(SList.getSelectedItem().toString()),new Double(XList.getSelectedItem().toString()),new Double(TList.getSelectedItem().toString()),
								new Double(rList.getSelectedItem().toString()),new Double(bList.getSelectedItem().toString()),new Double(vList.getSelectedItem().toString()),
								new Integer(nStList.getSelectedItem().toString()),new Integer(nSiList.getSelectedItem().toString()));
						label1.setText("The result of the options is "+result[0]+" which was calculating in "+result[1]+" by using the single thread algorithm");	

					}
					//Si on est en multi thread
					else{
						try {
							result=MonteCarloConcurrency.MonteCarloStandardOptionAVG("p",new Double(SList.getSelectedItem().toString()),new Double(XList.getSelectedItem().toString()),new Double(TList.getSelectedItem().toString()),
									new Double(rList.getSelectedItem().toString()),new Double(bList.getSelectedItem().toString()),new Double(vList.getSelectedItem().toString()),
									new Integer(nStList.getSelectedItem().toString()),new Integer(nSiList.getSelectedItem().toString()));
							label1.setText("The result of the options is "+result[0]+" which was calculating in "+result[1]+" by using the multiple thread algorithm");	

						} catch (NumberFormatException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} catch (InterruptedException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} catch (ExecutionException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}

					FinalBox.remove(bar);
				}
			
			}
		});

		//Action sur button "restart"
		restart.setMnemonic(KeyEvent.VK_I);//Listener of the button
		restart.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.getContentPane().remove(panel1);
				frame.getContentPane().add(panel); //on remet le formulaire
				frame.validate();
				frame.repaint();
			}
		});
		//Display the window.
		//frame.pack();
		frame.setSize(1000,350);
		frame.setVisible(true);
	}
	
	public static Object[][] FromFile() throws NumberFormatException, IOException{
		BufferedReader reader =  new BufferedReader(new FileReader("EuropeanOptions.txt"));

		//final Map<String, Integer> stocks = new HashMap<String, Integer>();
		//On trouve la ligne la + longue pour assigner la taile du tableau
		int max=0;
		String OptionsInfo = null;
		while ((OptionsInfo = reader.readLine()) != null) {			
			final String[] stockInfoData = OptionsInfo.split("-");
			if(stockInfoData.length>max)
				max=stockInfoData.length;
		}
		Object[][] tab = new Object[8][max];
		/******************Passer aux arrays????******************/


		reader =  new BufferedReader(new FileReader("EuropeanOptions.txt"));
		OptionsInfo=null;
		int line=0;
		while ((OptionsInfo = reader.readLine()) != null) {			
			final String[] stockInfoData = OptionsInfo.split("-");
			for(int i=0;i<stockInfoData.length; i++){
				if(line==0 || line==1 || line==6 || line==7){ //integer line 0:S, 1:X, 6:nSteps,7:nSimulations
					tab[line][i]=Integer.parseInt(stockInfoData[i]);
				}
				else	//Double line 2:r,3:T,4:b,5:v
					tab[line][i]=Double.parseDouble(stockInfoData[i]);
			}
			line++;
		}

		return tab;   

	}

	public static JProgressBar bar;


}
